package BookStore.view;

import BookStore.util.Myutil;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Sale extends JFrame {
    public
    JPanel searchmain;
    JPanel spanel1;
    JPanel spanel2;
    JPanel spanel3;

    JButton day;
    JButton month;
    JButton season;

    JTextField dayfield;
    JTextField monthfield;
    JTextField seasonfield;

    public Sale(){
        LayoutManager layoutManager = new GridLayout(4,1);
        setLayout(layoutManager);
        setTitle("销售额查询");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(400, 400, 485, 343);
        setLocationRelativeTo(null);
        searchmain= new JPanel(new GridLayout(4,1));
        day =new JButton("按日查询");
        day.setBounds(100, 10, 300, 100);

        day.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                try (java.sql.Connection conn = Myutil.getConnection()){

                    java.sql.Statement st = conn.createStatement();
                    String a = null;
                    String bid =  dayfield.getText();
                    PreparedStatement ps = null;
                    String sql="select * from SELL where day =?";
                    ps=conn.prepareStatement(sql);
                    ps.setString(1, bid);
                    ResultSet rs=ps.executeQuery();
                    while(rs.next()){
                        String DAY=rs.getString(1);
                        String MONTH=rs.getString(2);
                        String SEASON=rs.getString(3);
                        String VID=rs.getString(4);
                        String BID=rs.getString(5);
                        String PRICE=rs.getString(6);
                        String SHULIANG =rs.getString(7);

                        a="| 日："+DAY+"\t| 月: "+MONTH+"\t| 季节； "+SEASON+"\t| 顾客编号: "+VID+"\t| 书籍编号: "+BID+"\t| 价格:"+PRICE+"\t| 数量: "+SHULIANG;
                        System.out.println(a);

                    }
                    result result = new result(a.toString());

                    JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });

        month =new JButton("按月查询");
        month.setBounds(100, 40,300, 100);
        month.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                try (java.sql.Connection conn = Myutil.getConnection()){
                    String a=null;
                    java.sql.Statement st = conn.createStatement();
                    String name = monthfield.getText();
                    PreparedStatement ps = null;
                    String sql="select * from  SELL where month =?";
                    ps=conn.prepareStatement(sql);
                    ps.setString(1, name);
                    ResultSet rs=ps.executeQuery();
                    while(rs.next()){
                        String DAY=rs.getString(1);
                        String MONTH=rs.getString(2);
                        String SEASON=rs.getString(3);
                        String VID=rs.getString(4);
                        String BID=rs.getString(5);
                        String PRICE=rs.getString(6);
                        String SHULIANG =rs.getString(7);

                        a="| 日："+DAY+"\t| 月: "+MONTH+"\t| 季节； "+SEASON+"\t| 顾客编号: "+VID+"\t| 书籍编号: "+BID+"\t| 价格:"+PRICE+"\t| 数量: "+SHULIANG;
                        System.out.println(a);

                    }
                    result result = new result(a.toString());
                    JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);






                } catch (SQLException ex) {
                    ex.printStackTrace();

                }
            }

        });

        season=new JButton("按季查询");
        season.setBounds(100, 50, 300, 100);
        season.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                try (java.sql.Connection conn = Myutil.getConnection()){

                    java.sql.Statement st = conn.createStatement();
                    String press =  seasonfield.getText();
                    String a=null;
                    PreparedStatement ps = null;
                    String sql="select * from SELL where season =?";
                    ps=conn.prepareStatement(sql);
                    ps.setString(1, press);
                    ResultSet rs=ps.executeQuery();
                    while(rs.next()){
                        String DAY=rs.getString(1);
                        String MONTH=rs.getString(2);
                        String SEASON=rs.getString(3);
                        String VID=rs.getString(4);
                        String BID=rs.getString(5);
                        String PRICE=rs.getString(6);
                        String SHULIANG =rs.getString(7);

                        a="| 日："+DAY+"\t| 月: "+MONTH+"\t| 季节； "+SEASON+"\t| 顾客编号: "+VID+"\t| 书籍编号: "+BID+"\t| 价格:"+PRICE+"\t| 数量: "+SHULIANG;
                        System.out.println(a);

                    }
                    result result = new result(a.toString());
                    JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);

                } catch (SQLException ex) {
                    ex.printStackTrace();

                }
            }

        });

        spanel1=new JPanel();
        spanel2 =new JPanel();
        spanel3=new JPanel();

        dayfield = new JTextField(8);
        monthfield = new JTextField(8);
        seasonfield = new JTextField(8);
        spanel1.add(day);
        spanel1.add(dayfield);
        spanel2.add(month);
        spanel2.add(monthfield);
        spanel3.add(season);
        spanel3.add(seasonfield);

        add(spanel1);
        add(spanel2);
        add(spanel3);

        setVisible(true);
    }

}

